<?php

namespace app\api\Service;

use think\Db;
use app\common\library\Auth;

class CouponService
{
    // 获取优惠卷列表
    public static function getCouponList($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $limit = isset($data['limit']) ? $data['limit'] : 10;

        $coupon_ids = \app\admin\model\CouponUser::where(['user_id'=>$user_id])->column('coupon_id');
        if($coupon_ids){
            $where = ['id'=>['not in',$coupon_ids],'status'=>'normal','expiretime'=>['>',time()]];
        }else{
            $where = ['status'=>'normal','expiretime'=>['>',time()]];
        }

        $list = \app\admin\model\Coupon::where($where)->order('createtime desc')->field('id,name,reduction_amount,discount_amount,expiretime')->paginate($limit)->toArray();
        foreach($list['data'] as &$item){
            $item['expiretime_text'] = date('Y-m-d H:i:s', $item['expiretime']);
        }
        return ['code'=>1,'msg'=>'查询成功','data'=>['total'=>$list['total'],'list'=>$list['data']]];
    }

    // 领取优惠卷
    public static function collectCoupon($data)
    {
        try{
            if(!$data['ids']) return ['code'=>0, 'msg'=>'无效参数','data'=>''];
            $auth = Auth::instance();
            $ids = explode(',', $data['ids']);
            $params = [];
            foreach($ids as $id){

                $coupon = \app\admin\model\Coupon::where(['id'=>$id])->field('name,reduction_amount,discount_amount,expire_days')->find();
                if( !$coupon ) continue;

                $exist = \app\admin\model\CouponUser::where(['coupon_id'=>$id, 'user_id'=>$auth->id])->value('id');
                if( $exist ) return ['code'=>0, 'msg'=>'不要重复领取','data'=>''];
                $expiretime = strtotime("+{$coupon['expire_days']} days", strtotime(date('Y-m-d').'23:59:59'));

                $params[] = [
                    'user_id' => $auth->id,
                    'coupon_id' => $id,
                    'name' => $coupon['name'],
                    'reduction_amount' => $coupon['reduction_amount'],
                    'discount_amount' => $coupon['discount_amount'],
                    'createtime' => time(),
                    'updatetime' => time(),
                    'expiretime' => $expiretime,
                    'status' => 1
                ];
            }
            if(count($params) > 0){
                $result = \app\admin\model\CouponUser::insertAll($params);
            }else{
                return ['code'=>0,'msg'=>'优惠劵不存在','data'=>''];
            }

            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage(),'data'=>''];
        }
        if($result){
            return ['code'=>1,'msg'=>'操作成功','data'=>''];
        }else{
            return ['code'=>0,'msg'=>'操作失败','data'=>''];
        }
    }

    // 我的获取优惠卷列表
    public static function getMyCouponLists($data)
    {
        $auth = Auth::instance();
        $user_id = $auth->id;
        $limit = isset($data['limit']) ? $data['limit'] : 10;
        $status = isset($data['status']) ? $data['status'] : 1;
        $where = ['user_id'=>$user_id,'status'=>$status];

        $list = \app\admin\model\CouponUser::where($where)->field('id,name,reduction_amount,discount_amount,expiretime')->paginate($limit)
            ->each(
            function($item){
                $item->expiretime_text = date('Y-m-d H:i:s', $item['expiretime']);
                unset($item['expiretime']);
                return $item;
            }
        );

        return ['code'=>1,'msg'=>'查询成功','data'=>$list];
    }
}
